package com;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Test2 {

    public static <Sting> void main(String[] args) throws IOException {

        String[] array = {"小明","小红","小黄","小兰","小明","小兰"};

        Map<String, Integer> map = findRepelition(array);
        if (map != null) {
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                if (entry.getValue() > 1) {
                    System.out.println( entry.getKey() + " 重复出现" + entry.getValue() + "次");
                } else {
                    System.out.println( entry.getKey() + " 只出现1次 无重复");
                }
            }
        }
    }

    private static Map<String, Integer> findRepelition(String[] arr) {
        Map<String, Integer> map = new HashMap<>();  //创建哈希map
        if (arr == null || arr.length <= 0) {//如果为空，直接返回
            return null;
        }
        for (int i = 0; i < arr.length; i++) {//遍历值名字作为KEY
            if (map.containsKey(arr[i])) {
                map.put(arr[i], map.get(arr[i]) + 1);//判断是否有key，有则value+1
            } else {
                map.put(arr[i], 1);
            }
        }
        return map;
    }
}
